<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.accessKeyId"
        style="width: 200px"
        class="filter-item"
        :placeholder="$t('device.list.searchAccessKeyId')"
      />
      <el-select
        v-model="listQuery.productId"
        style="width: 200px"
        class="filter-item"
        :placeholder="$t('device.list.chooseProduct')"
      >
        <el-option v-for="(item,index) in productList" :key="index" :label="item.title" :value="item.id" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('device.list.search') }}</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">{{ $t('device.list.create') }}</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      :element-loading-text="$t('device.list.searching')"
      border
      fit
      highlight-current-row
    >

      <el-table-column align="center" :label="$t('device.list.id')" width="80" prop="id" />

      <el-table-column align="center" :label="$t('device.list.productId')" width="80" prop="productId" />

      <el-table-column align="center" :label="$t('device.list.productTitle')" min-width="100" prop="productTitle" />

      <el-table-column align="center" :label="$t('device.list.title')" min-width="100" prop="title" />

      <el-table-column align="center" :label="$t('device.list.status')" width="90" prop="status">
        <template slot-scope="scope">
          <el-tag :type="statusColor(scope.row.status)">{{ statusFilter(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="AccessKey Id" prop="accessKeyId" />

      <el-table-column align="center" label="AccessKey Secret">
        <template slot-scope="scope">
          <el-button size="mini" @click="showAccessKeySecret(scope.row)">显示</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('device.list.lastIp')" prop="lastIp" />

      <el-table-column align="center" :label="$t('device.list.gmtActive')" prop="gmtActive">
        <template v-if="scope.row.gmtActive" slot-scope="scope">{{ scope.row.gmtActive | formatTime }}</template>
      </el-table-column>

      <el-table-column align="center" :label="$t('device.list.gmtCreate')" prop="gmtCreate">
        <template slot-scope="scope">{{ scope.row.gmtCreate | formatTime }}</template>
      </el-table-column>

      <el-table-column align="center" :label="$t('device.list.operation')" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="handleDataPoint(scope.row)">{{ $t('device.list.dataPoint') }}</el-button>
          <el-button type="primary" size="mini" @click="handleShowQRCode(scope.row)">{{ $t('device.list.qrCode') }}</el-button>
          <el-button type="danger" style="margin:5px" size="mini" @click="handleDelete(scope.row)">{{ $t('device.list.deletE') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <el-tooltip placement="top" :content="$t('device.list.toTop')">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

    <el-dialog
      title="AccessKey Secret"
      :visible.sync="accessKeySecretVisible"
      width="30%"
    >
      <span>{{ accessKeySecret }}</span>
    </el-dialog>

    <!-- 添加对话框 -->
    <el-dialog :title="$t('device.list.create')" :visible.sync="createDeviceDialogVisible">
      <el-form
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item :label="$t('device.list.product')" prop="type">
          <el-select v-model="selectedProductId" :placeholder="$t('device.list.productPlaceholder')">
            <el-option v-for="(item, index) in productList" :key="index" :label="item.title" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDeviceDialogVisible = false">{{ $t('device.list.cancel') }}</el-button>
        <el-button :loading="submiting" type="primary" @click="doCreate">{{ $t('device.list.confirm') }}</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="$t('device.list.qrCode')" :visible.sync="qrcodeDialogVisible" width="25%">
      <vue-qr style="width: 100%" :bg-src="qrcodeConfig.imagePath" :logo-src="qrcodeConfig.imagePath" :text="qrcodeConfig.value" :color-dark="qrcodeConfig.BYTE_POS" :size="500" />
    </el-dialog>

    <el-dialog :title="$t('device.list.dataPoint')" :visible.sync="dataPointDialogVisible">
      <el-table
        :data="dataPointList"
        style="width: 100%"
      >
        <el-table-column
          prop="title"
          :label="$t('device.list.dataPointTitle')"
        />
        <el-table-column
          prop="idTag"
          :label="$t('device.list.dataPointIdTag')"
        />
        <el-table-column
          prop="type"
          :label="$t('device.list.dataPointType')"
        >
          <template slot-scope="scope">
            {{ $t(dataPointTypeFilter(scope.row.type).title) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="value"
          :label="$t('device.list.dataPointValue')"
        />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination'
import { listProduct } from '@/api/product'
import { listDevice, createDevice, showDeviceAccessSecret, listDataPoint } from '@/api/device'
import VueQr from 'vue-qr'

const dataPointTypeDic = [{ type: 1, title: 'product.upsert.datatype.integer' }, { type: 2, title: 'product.upsert.datatype.string' }, { type: 3, title: 'product.upsert.datatype.float' }]
const statusDic = { 1: 'device.list.inactive', 2: 'device.list.offline', 3: 'device.list.online', 4: 'device.list.scrap' }
const statusColorDic = { 1: 'warning', 2: 'info', 3: 'success', 4: 'danger' }
export default {
  name: 'ProductList',
  components: { BackToTop, Pagination, VueQr },
  data() {
    return {
      // 常量
      dataPointTypeDic,
      // 变量
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        productId: '',
        accessKeyId: ''
      },
      total: 3,
      listLoading: false,
      list: [],
      productList: [],
      accessKeySecret: '',
      accessKeySecretVisible: false,
      // 表单提交中
      submiting: false,
      createDeviceDialogVisible: false,
      selectedProductId: undefined,
      // 二维码
      qrcodeDialogVisible: false,
      qrcodeConfig: {
        value: 'www.baidu.com',
        imagePath: require('../../assets/logo.png'),
        BYTE_POS: 'green'
      },
      // 数据点
      dataPointDialogVisible: false,
      dataPointList: []
    }
  },
  created() {
    this.init()
    this.getList()
  },
  methods: {
    // 初始化
    init() {
      listProduct({
        pageSize: 15
      })
        .then(res => {
          this.productList = res.data.items
          if (res.data.total > 15) {
            // TODO 若总数超过了15，则转用可搜索下拉框
          }
        })
    },
    // 查询
    getList() {
      this.listLoading = true
      listDevice(this.listQuery)
        .then(res => {
          this.list = res.data.items
          this.total = res.data.total
          this.listLoading = false
        }).catch(failres => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    // 查看数据点
    handleDataPoint(row) {
      this.dataPointDialogVisible = true
      listDataPoint(row.id)
        .then(res => {
          this.dataPointList = res.data
        })
    },
    // 删除
    handleDelete() {

    },
    // 增加
    handleCreate() {
      this.createDeviceDialogVisible = true
      this.selectedProductId = undefined
    },
    doCreate() {
      this.createDeviceDialogVisible = false
      if (!this.selectedProductId) {
        this.$notify({
          title: this.$t('message.fail'),
          message: this.$t('device.list.chooseProductPlease'),
          type: 'error'
        })
        return
      }
      this.submiting = true
      createDevice(this.selectedProductId)
        .then(res => {
          this.$notify({
            title: this.$t('message.success'),
            message: this.$t('device.list.createDeviceSuccess'),
            type: 'success'
          })
          this.getList()
          this.submiting = false
        }).catch(failres => {
          this.submiting = false
        })
    },
    statusColor(code) {
      return statusColorDic[code]
    },
    showAccessKeySecret(row) {
      // 获取设备accessKey Secret
      showDeviceAccessSecret(row.id)
        .then(res => {
          this.accessKeySecret = res.data
          this.accessKeySecretVisible = true
        })
    },
    statusFilter(code) {
      return this.$t(statusDic[code])
    },
    creatQrCode(text) {
      this.qrcodeConfig.value = text
    },
    handleShowQRCode(row) {
      this.qrcodeDialogVisible = true
      this.creatQrCode(row.accessKeyId)
    },
    dataPointTypeFilter(type) {
      return dataPointTypeDic[type - 1]
    }
  }
}
</script>
